<template>
  <div class="login_container">
      <div class="login_box">
          <!-- 登录头像 -->
            <div class="login_sign">
                <img src="../assets/logo.png" alt="">
            </div>
          <!-- 登录表单 -->
        <el-form ref="loginFormRef" label-width="0" class="login_from" :model="login_form" :rules="login_form_roles">
            <!-- 用户框 -->
            <el-form-item prop="username">
                <el-input v-model="login_form.username" prefix-icon="iconfont icon-yonghu"></el-input>
            </el-form-item>
            <!-- 密码框 -->
            <el-form-item prop="password">
                <el-input v-model="login_form.password" prefix-icon="iconfont icon-mima" type="password"></el-input>
            </el-form-item>
            <!-- 按钮 -->
            <el-form-item class="btns">
                <el-button type="primary" @click="loginFormValid">登录</el-button>
                <el-button type="info" @click="loginFromReset">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      // 表单用户名和密码绑定
      login_form: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则对象
      login_form_roles: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮，重置表单
    loginFromReset () {
      this.$refs.loginFormRef.resetFields()
    },
    // 验证表单内容
    loginFormValid () {
      this.$refs.loginFormRef.validate(async ralid => {
        if (!ralid) return
        const { data: res } = await this.$http.post('login', this.login_form)
        if (res.meta.status !== 200) return this.$Message({ message: '登录失败', type: 'warning' })
        this.$Message({ message: '登录成功', type: 'success' })
        // 保存token到sesssionStorage
        window.sessionStorage.setItem('token', res.data.token)
        // 跳转到home页面
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .login_container{
      position: relative;
      background-color: #2b4b6b;
      height: 100%;
  }
  .login_box{
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
      background-color: #ffff;
      width: 450px;
      height: 300px;
      border-radius: 5px;
  }
  .login_sign{
      position: absolute;
      left: 50%;
      transform: translate(-50%,-50%);
      height: 130px;
      width: 130px;
      border: 1px solid #cccc;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 5px #cccc;
      background-color: #ffff;
      img{
          width: 100%;
          height: 100%;
          border-radius: 50%;
          background-color: #cccc;
      }
  }
  .btns{
      display: flex;
      justify-content: flex-end;
  }
  .login_from{
      position: absolute;
      bottom: 0;
      width: 100%;
      box-sizing: border-box;
      padding: 15px;
  }
</style>
